package com.hanamaki.demo02;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class AppiumSwipe02 {
    static AndroidDriver<WebElement> driver;
    @BeforeMethod
    public void setUp() throws MalformedURLException {
//        1. 创建配置对象   new
        DesiredCapabilities cp = new DesiredCapabilities();
//          添加配置信息
        cp.setCapability("deviceName", "127.0.0.1:62001");
        cp.setCapability("platformName", "Android");
        cp.setCapability("appPackage", "com.tencent.mobileqq");
        cp.setCapability("appActivity", "com.tencent.mobileqq.activity.SplashActivity");
        cp.setCapability("noReset", "true");
//        如何启动？
//        驱动 对象
        URL url = new URL("http://localhost:4723/wd/hub");
//        注册 需要打开的 驱动 以及url 以及 对应的软件
        driver = new AndroidDriver<>(url, cp);

        WebDriver.Timeouts timeouts = driver.manage().timeouts();
        timeouts.implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() throws InterruptedException {

//        5.0 之后的版本
//        在 5.0 之后 需要接住 PointOption对象来滑动
//        需要使用  事件（手）
        TouchAction hand = new TouchAction(driver);
        /*
        *  210  600
        *  360  600
        *  500  600
        *  360  750
        *  210  890
        *  360  890
        *  500  890
        * */

//        设置 需要滑动的坐标
        PointOption point1 = PointOption.point(210, 600);  // ctrl + d
        PointOption point2 = PointOption.point(360, 600);
        PointOption point3 = PointOption.point(500, 600);
        PointOption point4 = PointOption.point(360, 750);
        PointOption point5 = PointOption.point(210, 890);
        PointOption point6 = PointOption.point(360, 890);
        PointOption point7 = PointOption.point(500, 890);
//
        Thread.sleep(3000);

//        执行滑动 从第一个点滑动 到 第二个点  需要多长时间
        Duration duration = Duration.ofMillis(800);
        WaitOptions waitOptions = WaitOptions.waitOptions(duration);
        hand.press(point1)
                .waitAction(waitOptions)
                .moveTo(point2)
                .moveTo(point3)
                .moveTo(point4)
                .moveTo(point5)
                .moveTo(point6)
                .moveTo(point7)
                .release()
                .perform();
    }

    @AfterMethod
    public void release() {
//        driver.quit();
    }

}
